'''
https://leetcode.cn/problems/ipo/description/
'''
import heapq
from typing import List
class Solution:
    def findMaximizedCapital(self, k: int, w: int, profits: List[int], capital: List[int]) -> int:
        projects = sorted(zip(capital, profits))
        n = len(projects)

        heap = []    # 构建 -利润的小顶堆。
        i = 0
        for _ in range(k):
            while i < n and projects[i][0] <= w:
                heapq.heappush(heap, -projects[i][1])
                i += 1
            if heap:
                w += -heapq.heappop(heap)
        return w
